package com.xintoucloud.gsv.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.gsv.entity.Dept;
import com.xintoucloud.gsv.entity.SuperVision;
import com.xintoucloud.gsv.entity.SuperVisionState;
import com.xintoucloud.gsv.entity.SupervisionType;
import com.xintoucloud.gsv.entity.User;
import com.xintoucloud.gsv.model.DataGrid;
import com.xintoucloud.gsv.model.SuperVisionDto;
import com.xintoucloud.gsv.service.SuperVisionService;

@Controller
public class MessageController {
	@Autowired
	private SuperVisionService superVisionService;
	
	@RequestMapping("/showMessage")
	public String showMessage() {
		
		return "message";
		
	}
	@RequestMapping("/showSuperVisionMessage")
	@ResponseBody
	public DataGrid<SuperVisionDto> showSuperVisionMessage(Model m,HttpServletRequest request,HttpSession session) {
		//1.获取参数
		 String page = request.getParameter("page");// 获取当前页 
		 String size = request.getParameter("limit");// 获取每页信息显示的条数
		 User user=(User) session.getAttribute("User");
		 int did=user.getDept().getDid();
		 //2.数据库执行分页操作
		DataGrid<SuperVisionDto> superVisions=superVisionService.listMessagePage(Integer.parseInt(page), Integer.parseInt(size),did);
		//System.out.println("count:"+superVisions.getCount());
		m.addAttribute("superVisions", superVisions);
		return superVisions;
	}
	@PostMapping("/superVisionRegister")
	@ResponseBody
	public SuperVision superVisionStart(Integer svid,Integer sid,Integer did) {
		System.out.println("签收督办执行了");	
		//设置督办状态为已签收
		SuperVision superVision= superVisionService.getSuperVision(svid);
		SuperVisionState superVisionState=new SuperVisionState();
		superVisionState.setStid(3);
		superVision.setSuperVisionState(superVisionState);
		//设置签收时间
		Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        superVision.setRealConfirmTime(sdf.format(date));
        //设置原来的sid和did不变
        SupervisionType supervisionType=new SupervisionType();
		supervisionType.setSid(sid);
		superVision.setSupervisionType(supervisionType);
		Dept dept=new Dept();
		dept.setDid(did);
		superVision.setDept(dept);
		superVisionService.update(superVision);
		SuperVision superVision1= superVisionService.getSuperVision(svid);
		return superVision1;
		
	}
}
